package com.qianbei.view.errotip;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianbei.R;
import com.qianbei.bean.GetErroBean1;
import com.qianbei.bean.GetErroBean2;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.okhttp.RequeastBean;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.utilis.img.CanmlTools;
import com.qianbei.common.utilis.img.ImageTools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccidentActivity extends BaseActivity implements CanmlTools.CamerCallback {


    @Bind(R.id.taction_et)
    EditText tactionEt;
    @Bind(R.id.up_img)
    TextView upImg;
    @Bind(R.id.show_img)
    ImageView showImg;
    @Bind(R.id.taction_save)
    TextView tactionSave;
    private CanmlTools canmlTools = null;
    String id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gufei_erro_acitivty2);
        ButterKnife.bind(this);
        setTitle("事故上报");
        canmlTools = new CanmlTools(mContext, this);
    }
    @OnClick({R.id.up_img, R.id.taction_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.up_img:
                canmlTools.showCanmel(view.getId(), 0);
                break;
            case R.id.taction_save:
                if (TextUtils.isEmpty(tactionEt.getText().toString())) {
                    new MsgAlert().show("请输入事故说明");
                } else if(TextUtils.isEmpty(id)){
                    new MsgAlert().show("请上传图片");
                }else {
                    interrupt(id,tactionEt.getText().toString());
                }
                break;
        }
    }

    @Override
    public void camerPickphoto(String imageUri, int i) {
        Bitmap tempbBitmap = ImageTools.bitmapFromPath(imageUri,
                (int) ImageTools.getDensity() * 65,
                (int) ImageTools.getDensity() * 65);

        switch (i) {
            case R.id.up_img:
//                fileArry.add(imageUri);
//                upArry[0].file = imageUri;
                showImg.setImageDrawable(new BitmapDrawable(
                        getResources(), tempbBitmap));
//                upLoadImage(imageUri, new ApplyRegistActivity.HttpCall(0));
                Map<String, File> fileParams = new HashMap<>();
                File file = new File(imageUri);
                fileParams.put("file", file);
                ServerControl sc = new ServerControl(fileParams);
                sc.serverListener = new ServerControl.ServerListener() {
                    @Override
                    public void serverFinish(ServerResult sr) {

                        if (sr.isContinue) {
                            id = sr.bodyData.optString("message");
                            new MsgAlert().show("上传成功，请保存提交");
                        }
                    }
                };
                new ProgressDialog(mContext).startControlUPpic(sc);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        canmlTools.onresult(requestCode, data);
    }





    /**
     * 上报 异常报警 - 车辆满载
     */
    private void interrupt(String id ,String money) {
        ArrayList<String> photo_id  = new ArrayList<>();

        photo_id.add(id);
        ServerControl sc = new ServerControl(
                new RequeastBean("interrupt", "com.cygps.dubbo.WasteTransport.IRouteInfo",
                        new GetErroBean2(photo_id, money)));
        sc.serverListener = new ServerControl.ServerListener() {

            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    new MsgAlert().show("上报成功");
                    finish();
                }

            }
        };
        new ProgressDialog(mContext).OKpost(sc);
    }


}
